﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyWebUtils;
using TTQT;
using TTQT.TTQT_Code;
public partial class DMDotNhapHang : MasterControl//System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Text = string.Empty;
        if (!Page.IsPostBack)
        {
            txtNgay.Value = DateTime.Now.ToString("dd/MM/yyyy");
            bindGrid();
            visiblePanels(true, false);
            return;
        }
    }
    private void bindGrid()
    {
        try
        {
            ds.SelectCommandType = SqlDataSourceCommandType.Text;
            ds.SelectCommand = "select id,MaDot,TenDot,isnull(convert(varchar(10),Ngay,103),' ') Ngay,ghiChu from DMDotNhapHang where MaKho='" + UserInfo.MaKho + "'";
            datagrid.DataSourceID = "ds";
            datagrid.DataBind();
        }
        catch (Exception)
        {
        }
    }
    private void visiblePanels(bool visibleGrid, bool visibleEdit)
    {
        this.pnGroupGrid.Visible = visibleGrid;
        this.pnEdit.Visible = visibleEdit;
    }
    protected void cmdAddNew_Click(object sender, EventArgs e)
    {
        txtID.Enabled = true;
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(false, true);
        this.txtID.Text = string.Empty;
        this.txtTenDot.Text = string.Empty;
        this.txtNgay.Value = DateTime.Now.ToString("dd/MM/yyyy");
    }

    protected void cmdUpdate_Click(object sender, EventArgs e)
    {
        //try
        //{
        //    if (txtID.Text.ToUpper().Contains("_KH"))
        //    {
        //        MyWebUtil.alert(this.Page,"Tên PGD không được chứa chuỗi _KH");
        //        return;
        //    }
        //}
        //catch { }
        try
        {
            // Nếu là sửa Thì ID sẽ khác rỗng

            if (txtID.Text.Trim().Length>0)
            {
                ds.UpdateCommand = "update DMDotNhapHang set TenDot=N'" + txtTenDot.Text + "',Ngay='"+TTQTCommon.convertDateString(txtNgay.Value)+"',ghiChu=N'"+txtGhiChu.Text+"' where ID=" + txtID.Text+" and maKho='"+UserInfo.MaKho+"'";
                ds.Update();
                bindGrid();
                visiblePanels(true, false);
                lblError.Text = "";
            }
            // Nếu là thêm mới thì ID sẽ rỗng
            else
            {
                ds.InsertCommand = "insert into DMDotNhapHang(TenDot,Ngay,GhiChu,MaKho) values(N'" + txtTenDot.Text + "','" + TTQTCommon.convertDateString(txtNgay.Value) +"',N'" + txtGhiChu.Text + "','" + UserInfo.MaKho + "')";
                ds.Insert();
                bindGrid();
                visiblePanels(true, false);
            }
        }
        catch (Exception ex)
        {
            lblUpdateStatus.Attributes.Add("style", "color:red;font-weight:bold;");
            this.lblUpdateStatus.Text = "Trung ma. Nhap lai! <br />"+ex.ToString();
            //Log methods here
        }
    }
    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        lblUpdateStatus.Text = "";
        visiblePanels(true, false);
    }
    protected void datagrid_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.CommandName == "cmdEdit")
        {
            txtID.Text = e.Item.Cells[0].Text.Trim();
            txtTenDot.Text = e.Item.Cells[1].Text.Trim();
            txtNgay.Value = e.Item.Cells[2].Text.Trim();
            txtGhiChu.Text = e.Item.Cells[3].Text.Trim();
            visiblePanels(false, true);
            /*
            ds.SelectCommandType = SqlDataSourceCommandType.Text;
            ds.SelectCommand = "select * from MaDot where MaDot=" + e.Item.Cells[0].Text;
            DataTable hehe = ((DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty)).ToTable();

            hehe.Rows[0][1].ToString();
            this.txtID.Text = hehe.Rows[0][0].ToString();
            this.txtTenDot.Text = hehe.Rows[0][1].ToString();
            */
            // Save old info
            txtID.Enabled = false;
        }
        else if (e.CommandName == "cmdDelete")
        {
            DataView kq;
            ds.SelectCommand = "select count(*) from khohang where madot='" + e.Item.Cells[0].Text + "'";
            kq = (DataView)ds.Select(System.Web.UI.DataSourceSelectArguments.Empty);
            
            if(!kq.Table.Rows[0][0].ToString().Equals("0"))
            {
                MyWebUtil.alert(this.Page,"Đợt này vẫn còn hàng tồn nên không xóa được.");
                bindGrid();
                return;
            }

            ds.DeleteCommand = "delete DMDotNhapHang where id='" + e.Item.Cells[0].Text+"'";
            ds.Delete(); 
            bindGrid();
            
        }
    }
    protected void datagrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex < 0) return;
        Button cmdDel = (Button)e.Item.FindControl("cmdDelete");
        if (cmdDel != null) cmdDel.Attributes.Add("onclick", "delete_Confirm");
    }
    protected void datagrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        datagrid.CurrentPageIndex = e.NewPageIndex;
        bindGrid();
    }
}
